<?php
//获得商品规格属性和库存等数据
class GoodSpecEditModel extends Model{
	public $gid;
	//获得商品规格的属性
	public function GoodSpec($gid){
		$this->gid=$gid;
 		$cid=Q("cid",0,"intval");
 		//查找所属商品类型
 		$gtid=M("category")->where(array("cid"=>$cid))->find("gtid");
 		//查找商品类型里面的属性
 		$attrClass=M("g_attr_class")->where(array("add_goods_type_aid"=>$gtid['gtid'],'is_spec'=>1))->all();
 		//查找库存数据
	      $stockData=M("stock")->where(array("goods_gid"=>$this->gid))->all();
	      foreach($stockData as $m=>$stock){
	      	$st_id=$stock['st_id'];
	      	if(!is_null($attrClass)){
			foreach($attrClass as $n=>$attr){
		 			$attrValue=M("g_arr_value")->where(array("g_attr_class_ac_id"=>$attr["ac_id"]))->all();
		 			$show_type=$attr["show_type"];
		 			if(method_exists($this,$show_type)){
		 				$html=$this->$show_type($attrValue,$st_id);
		 					}
		 			$stockData[$m]['html'][]['spec']=$html;
		 			$stockData[$m]['name'][]['name']=$attr['attr_name'];
		 				}
	     	 		}
	     		 }
		return $stockData; 		
	}

	//规格属性值类型是box
	public function box($attrValue,$st_id){
	//查找库存属性关联表
	$stockAttrData=M("stock_attr")->where(array("stock_st_id"=>$st_id))->all();
	// p($stockAttrData);die;
             // $GoodsAttrData=M("goods_attr")->where(array("gid"=>$this->gid))->all();
		$html="<select name='spec"."[".$attrValue[0]['g_attr_class_ac_id']."]"."[]'>";
             	//将属性值组合成一个下拉框
		foreach($attrValue as $attr){
			// p($attr);die;
			foreach($stockAttrData as $stockAttr){
				if($stockAttr['attr_value_av_id']==$attr['av_id']){
					$html.="<option value='{$attr['av_id']}|{$attr['av_name']}' selected=''>{$attr['av_name']}</option>";
					continue 2;//跳出两层循环
				}
			}
			$html.="<option value='{$attr['av_id']}|{$attr['av_name']}'>{$attr['av_name']}</option>";
		}
             $html.="</select>";
	   return $html;
	}


}